package org.jeecg.modules.quartz.job;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.data.entity.TDataStatistics;
import org.jeecg.modules.data.mapper.TDataReportMapper;
import org.jeecg.modules.data.mapper.TDataStatisticsMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@Slf4j
public class ReportJob implements Job {
    @Resource
    private TDataReportMapper reportMapper;
    @Resource
    private TDataStatisticsMapper statisticsMapper;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("===========执行报表任务开始===========");

        String investigationId = "1933344520448061441";
        List<String> cantCodelist = reportMapper.getCantCode(investigationId);
        List<String> areaList = new ArrayList<>();
        List<String> cityList = new ArrayList<>();
        List<String> provList = new ArrayList<>();
        for (String cantCode : cantCodelist) {
            String[] cantCodes = cantCode.split(":");
            // 区
            areaList.add(cantCodes[2]);
            // 市
            cityList.add(cantCodes[1]);
            // 省
            provList.add(cantCodes[0]);
        }
        TDataStatistics param = new TDataStatistics();
        param.setYear(2025);
        param.setInvestigationId(investigationId);
        // 更新区数据
        for (String area : areaList) {
            param.setCantCode(area);
            TDataStatistics query = statisticsMapper.selectDataByCantCode(param);
            // 更新数据
            TDataStatistics upParam = new TDataStatistics();
            upParam.setId(query.getId());
            upParam.setTownCount(reportMapper.getTownCount(area, investigationId));
            upParam.setSamplingPointCount(reportMapper.getPointCount(area, investigationId));
            BigDecimal averageLossRate = reportMapper.getAverageLossRate(area, investigationId);
            upParam.setAverageLossRate(averageLossRate);
            statisticsMapper.updateById(upParam);
        }
        // 更新市数据
        Set<String> uniqueSetC = new LinkedHashSet<>(cityList);
        cityList.clear(); // 清空原列表
        cityList.addAll(uniqueSetC); // 添加去重后的数据
        for (String city : cityList) {
            param.setCantCode(city);
            TDataStatistics query = statisticsMapper.selectDataByCantCode(param);
            // 更新数据
            TDataStatistics upParam = new TDataStatistics();
            upParam.setId(query.getId());
            upParam.setTownCount(reportMapper.getTownCount(city, investigationId));
            upParam.setSamplingPointCount(reportMapper.getPointCount(city, investigationId));
            BigDecimal averageLossRate = reportMapper.getAverageLossRate(city, investigationId);
            upParam.setAverageLossRate(averageLossRate);
            statisticsMapper.updateById(upParam);
        }
        // 更新省数据
        Set<String> uniqueSet = new LinkedHashSet<>(provList);
        provList.clear(); // 清空原列表
        provList.addAll(uniqueSet); // 添加去重后的数据
        for (String prov : provList) {
            param.setCantCode(prov);
            TDataStatistics query = statisticsMapper.selectDataByCantCode(param);
            // 更新数据
            TDataStatistics upParam = new TDataStatistics();
            upParam.setId(query.getId());
            upParam.setTownCount(reportMapper.getTownCount(prov, investigationId));
            upParam.setSamplingPointCount(reportMapper.getPointCount(prov, investigationId));
            BigDecimal averageLossRate = reportMapper.getAverageLossRate(prov, investigationId);
            upParam.setAverageLossRate(averageLossRate);
            statisticsMapper.updateById(upParam);
        }

        System.out.println("===========执行报表任务结束===========");
    }
}
